import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_base_app/widgets/dialog/base/base_dialog.dart';
import 'package:flutter_base_framework/base_framework_export.dart';
import 'package:flutter_common_utils/common_utils_export.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:flutter_base_app/widgets/dialog/popup/popup_select.dart';

import '../../config/app_colors.dart';

class CommonSelectDialog<T> extends BaseDialog {
  //列表显示数据
  final List<T> data;

  //已经选择的项
  final T? selected;
  final Future<bool> Function(T)? onAsyncSelect;
  final ValueChanged<T>? onSelect;
  final void Function()? onDismissCall;
  final double? width;
  final double? height;
  final String? title;
  final bool? backDismiss;

  //是否显示取消按钮
  final bool isShowCancel;

  //是否显示右上角关闭按钮
  final bool isShowClose;

  CommonSelectDialog({
    super.key,
    required this.data,
    this.selected,
    this.title,
    this.width,
    this.height,
    this.isShowCancel = false,
    this.isShowClose = false,
    this.backDismiss,
    this.onSelect,
    this.onAsyncSelect,
    this.onDismissCall,
  });

  @override
  State createState() => _CommonSelectDialogState<T>();

  @override
  void show() {
    SmartDialog.show(
      tag: tag,
      onDismiss: onDismiss,
      onMask: onMask,
      alignment: Alignment.bottomCenter,
      clickMaskDismiss: clickMaskDismiss,
      backType: backDismiss == null ? null : (backDismiss == true ? SmartBackType.normal : SmartBackType.block),
      builder: (context) => this,
    );
  }

  @override
  void onDismiss() {
    super.onDismiss();
    onDismissCall?.call();
  }
}

class _CommonSelectDialogState<T> extends BaseDialogState<CommonSelectDialog<T>> {
  _CommonSelectDialogState() : super();
  var selectIndex = 0;
  late FixedExtentScrollController _controller;

  @override
  void initState() {
    super.initState();
    selectIndex = widget.selected == null ? 0 : widget.data.indexOf(widget.selected as T).clamp(0, widget.data.length - 1);
    _controller = FixedExtentScrollController(initialItem: selectIndex);
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);

    return BaseDialogWidget(
      height: widget.height ?? 300,
      width: widget.width ?? AppUtils.width,
      title: widget.title ?? "",
      borderRadius: BorderRadius.vertical(top: Radius.circular(10)),
      onCloseClick: widget.isShowClose ? dismiss : null,
      child: Padding(
        padding: const EdgeInsets.only(left: 8, right: 8, bottom: 20),
        child: Column(
          children: [
            Expanded(
              child: CupertinoPicker.builder(
                scrollController: _controller,
                // 每个选项的高度
                itemExtent: 40,
                childCount: widget.data.length,
                onSelectedItemChanged: (int index) {
                  selectIndex = index;
                },
                selectionOverlay: Column(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    //水平渐变线
                    _buildHorizontalLine(),
                    //水平渐变线
                    _buildHorizontalLine(),
                  ],
                ),
                itemBuilder: (BuildContext context, int index) {
                  final data = widget.data[index];
                  var showText = "";
                  if (data is String) {
                    showText = data;
                  } else if (data is IShowText) {
                    showText = data.showText();
                  } else {
                    showText = data.toString();
                  }
                  return Center(child: Text(showText, style: TextStyle(fontSize: 16, color: ColorsApp.black)));
                },
              ),
            ),
            //两个按钮
            DialogCommonAction(
                showCancel: widget.isShowCancel,
                onResult: (it) async {
                  if (it) {
                    if (widget.onAsyncSelect != null) {
                      if (await widget.onAsyncSelect!(widget.data[selectIndex])) {
                        dismiss();
                      }
                    } else if (widget.onSelect != null) {
                      widget.onSelect!(widget.data[selectIndex]);
                      dismiss();
                    } else {
                      dismiss();
                    }
                  } else {
                    dismiss();
                  }
                }),
          ],
        ),
      ),
    );
  }

  Widget _buildHorizontalLine() {
    return Container(
      height: 1,
      decoration: BoxDecoration(
        gradient: LinearGradient(
          begin: Alignment.centerLeft,
          end: Alignment.centerRight,
          colors: [Colors.transparent, ColorsApp.black_3C, Colors.transparent],
        ),
      ),
    );
  }
}
